package com.app.twth.ui.mine.version;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.app.MyApplication;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.utils.ToastUtil;
import com.app.twth.widget.DownloadingDialog;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;

import java.io.File;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class VersionViewModel extends BaseAndroidViewModel<VersionDataModel> {
    public MutableLiveData<Version> versionLiveData = new MutableLiveData<>();
    public MutableLiveData<File> fileLiveData = new MutableLiveData<>();
    private String TAG = getClass().getSimpleName();

    public VersionViewModel(@NonNull Application application) {
        super(application);
    }

    public void getNewAppInfo() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        mModel.getNewAppInfo(new JsonCallBack<BaseResponseEntity<Version>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<Version>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<Version> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    versionLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    DownloadingDialog myDialogProgress;

    public void getApk(String url, Context context) {
        myDialogProgress = new DownloadingDialog(context);
        myDialogProgress.setCanceledOnTouchOutside(false);
        myDialogProgress.show();

        mModel.getApk(url, new FileCallback(MyApplication.getAppInstance().getExternalCacheDir().getPath(), "twth.apk") {
            @Override
            public void onSuccess(Response<File> response) {
                if (myDialogProgress != null) {
                    myDialogProgress.dismiss();
                }
                File file = response.body();
                if (file != null) {
                    Log.e(TAG, "file.getPath():" + file.getPath());
                    fileLiveData.setValue(file);
                }
            }

            @Override
            public void onError(Response<File> response) {
                super.onError(response);
                if (myDialogProgress != null) {
                    myDialogProgress.dismiss();
                }
                try {
                    ToastUtil.showToast("Network Error, Please Try Again Later");
                    Log.e(TAG, "请求服务器失败，请稍后再试" + response.message() + "----" + response.getException());
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtil.showToast("Network Error, Please Try Again Later");
                }
            }

            @Override
            public void downloadProgress(Progress progress) {
                super.downloadProgress(progress);
                Log.e(TAG, "downloadProgress():" + progress.fraction);
                if ((myDialogProgress != null)) {
                    myDialogProgress.setProgress((int) (progress.fraction * 100), 100);
                }
            }
        });
    }

}
